local M = {
	gMain = nil,
	gContent = nil,
}

local propUI = require("prop.propUI")
local position = require("prop.position")
local topHeight = 40

local function createAnchor(obj)
	local gAnchor = display.newGroup()

	local title = propUI.createText("Anchor")
	gAnchor:insert(title)

	local gX = propUI.createTextWithInput("X", obj.anchorX)
	gAnchor:insert(gX)
	gX.x, gX.y = title.x + title.width + 30, title.y

	local gY = propUI.createTextWithInput("Y", obj.anchorY)
	gAnchor:insert(gY)
	gY.x, gY.y = gX.x + gX.width + 15, gX.y

	return gAnchor
end
local function createboolean(obj, tSpe)
	local gBool = display.newText()
end

-- dislpay原生属性
local function origProperties(obj)
	local gOrig = display.newGroup()

	if not obj._properties then
		return gOrig
	end

	local tProps = json.decode(obj._properties)

	local tSpe = {
		x = {bind = "y", fact = position.createPosition},
		y = {bind = "x", fact = position.createPosition},
		anchorX = {bind = "anchorY", fact = createAnchor},
		anchorY = {bind = "anchorX", fact = createAnchor},
		tShowed = {}
	}
	local tDrawed = {}

	for key, v in pairs(tProps) do
		if not tSpe.tShowed[key] then
			if tSpe[key] then
				tSpe.tShowed[key] = true
				tSpe.tShowed[tSpe[key].bind] = true

				local g1 = tSpe[key].fact(obj)
				g1.anchorChildren = true
				g1.anchorX = 0
				g1.anchorY = 0
				g1.x = 10

				if gOrig.height == 0 then
					g1.y = 0
				else
					g1.y = gOrig.height + 10
				end
				gOrig:insert(g1)
			else

			end
		end
	end

	return gOrig
end

local function createProperties(obj)
	local g = display.newGroup()

	local gOrig = origProperties(obj)

	g:insert(gOrig)
	return g
end

function M.drawContent(event)
	if M.gContent then
		coreDisplay.cleanGroups(M.gContent)
		M.gContent = nil
	end

	local gPro = display.newGroup()
	local params = {width = 340, height = M.gMain.height - 20}
	local bgRect = display.newRect(0, 0, params.width, params.height)
	bgRect.anchorX = 0
	bgRect.anchorY = 0
	bgRect:setFillColor(unpack(colorCfg[8]))
	gPro:insert(bgRect)

	local g = createProperties(event.target)
	gPro:insert(g)

	gPro.x = -gPro.width - 5
	gPro.y = 10

	M.gContent = gPro
	M.gMain:insert(M.gContent)
end

function M.new(params)
	M.gMain = dragBox.drawDragBox({
		width = 350,
		height = H - topHeight,
		dragSides = {0, 0, 1, 0}
	})
	M.gMain.x = W
	M.gMain.y = topHeight

	M.drawContent({target = gPlatform.gContent})

	eventDispatcher:addEventListener("chooseObj", M.drawContent)

	return M.gMain
end

return M